package com.vcredit.creditcat.web.netcredit;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vcredit.creditcat.api.IAuthNetCreditManager;
import com.vcredit.creditcat.api.ICustManager;
import com.vcredit.creditcat.dto.BankcardInitDto;
import com.vcredit.creditcat.dto.BankcardInitResult;
import com.vcredit.creditcat.dto.BankcardSubmitDto;
import com.vcredit.creditcat.dto.CreditLoginDto;
import com.vcredit.creditcat.dto.EmailDto;
import com.vcredit.creditcat.dto.InitDto;
import com.vcredit.creditcat.dto.NetCreditBaseResultDto;
import com.vcredit.creditcat.dto.QuestionFirstStepDto;
import com.vcredit.creditcat.dto.QuestionSecondDto;
import com.vcredit.creditcat.dto.RegisterFirstStepDto;
import com.vcredit.creditcat.dto.RegisterSecondStepDto;
import com.vcredit.creditcat.dto.RegisterThirdStepDto;
import com.vcredit.creditcat.dto.ReportDto;
import com.vcredit.creditcat.dto.ReportResponseDto;
import com.vcredit.creditcat.model.Cust;
import com.vcredit.creditcat.vo.JsonResult;
import com.vcredit.creditcat.web.app.BaseCreditCatController;
import com.vcredit.framework.annotation.InterfaceRequest;
import com.vcredit.framework.annotation.Log;

/**
 * 网络版征信相关处理
 */
@RestController
@RequestMapping("/netCredit")
public class NetCreditController extends BaseCreditCatController {
	@Reference(version = "1.0")
	private ICustManager custManager;

	@Reference(version = "1.0")
	private IAuthNetCreditManager authNetCreditManager;

	@InterfaceRequest(value = "征信初始化")
	@RequestMapping("/init")
	public InitDto init(HttpServletRequest request) throws Exception {
		Cust cust = custManager.findById(super.getCust(request).getCustId());
		return authNetCreditManager.init(cust.getLastProccessId(), cust.getCustId());
	}

	@InterfaceRequest(value = "跳转银联认证页面")
	@RequestMapping("/bankcard/init")
	public BankcardInitResult bankcardInit(@Valid BankcardInitDto input, BindingResult result,
			HttpServletRequest request) throws Exception {
		Cust cust = custManager.findById(super.getCust(request).getCustId());
		return authNetCreditManager.bankcardInit(cust.getCustId(), input);
		// response.getWriter().write((String)result.getResult());
	}

	@InterfaceRequest(value = "银联认证方式获取身份查询")
	@RequestMapping("/bankcard/submit")
	public NetCreditBaseResultDto bankcardSubmit(@Valid BankcardSubmitDto input, BindingResult result,
			HttpServletRequest request) throws Exception {
		Cust cust = custManager.findById(super.getCust(request).getCustId());
		return authNetCreditManager.bankcardSubmit(cust.getCustId(), input);
	}

	@InterfaceRequest(value = "征信登录")
	@RequestMapping("/login")
	public NetCreditBaseResultDto login(@Valid CreditLoginDto input, BindingResult result, HttpServletRequest request)
			throws Exception {
		Cust cust = custManager.findById(super.getCust(request).getCustId());
		return authNetCreditManager.login(cust.getLastProccessId(), cust.getCustId(), input);
	}

	@InterfaceRequest(value = "五个问题认证获取问题列表")
	@RequestMapping("/query/apply/1")
	public NetCreditBaseResultDto getQuestion(@Valid QuestionFirstStepDto input, BindingResult result,
			HttpServletRequest request) throws Exception {
		custManager.findById(super.getCust(request).getCustId());
		return authNetCreditManager.getQuestion(input);

	}

	@InterfaceRequest(value = "五个问题认证提交五个问题答案")
	@RequestMapping("/query/apply/2")
	public NetCreditBaseResultDto submitAnswer(@Valid QuestionSecondDto input, BindingResult result,
			HttpServletRequest request) throws Exception {
		Cust cust = custManager.findById(super.getCust(request).getCustId());
		return authNetCreditManager.sumbitAnswer(cust.getCustId(), input);
	}

	@InterfaceRequest(value = "征信账号注册")
	@RequestMapping("/register/1")
	public NetCreditBaseResultDto register(@Valid RegisterFirstStepDto input, BindingResult result,
			HttpServletRequest request) throws Exception {
		return authNetCreditManager.register(input);
	}

	@InterfaceRequest(value = "征信账号注册发送短信")
	@RequestMapping("/register/2")
	public NetCreditBaseResultDto registerVerifyMobile(@Valid RegisterSecondStepDto input, BindingResult result,
			HttpServletRequest request) throws Exception {
		Cust cust = custManager.findById(super.getCust(request).getCustId());

		return authNetCreditManager.registerVerifyMobile(cust.getCustId(), input);
	}

	@InterfaceRequest(value = "征信账号注册提交申请")
	@RequestMapping("/register/3")
	public NetCreditBaseResultDto registerSubmit(@Valid RegisterThirdStepDto input, BindingResult result,
			HttpServletRequest request) throws Exception {
		// Cust cust = custManager.findById(super.getCust(request).getCustId());
		return authNetCreditManager.registerSubmit(input);
	}

	@InterfaceRequest(value = "查询征信报告")
	@RequestMapping("/query/report")
	public NetCreditBaseResultDto summaryReport(@Valid ReportDto input, BindingResult result,
			HttpServletRequest request) throws Exception {
		Cust cust = custManager.findById(super.getCust(request).getCustId());
		return authNetCreditManager.summaryReport(cust.getLastProccessId(), cust.getCustId(), input);
	}

	@InterfaceRequest(value = "查询征信报告")
	@RequestMapping("/query/reportAll")
	@Log
	public ReportResponseDto reportAll(HttpServletRequest request) throws Exception {
		Cust cust = custManager.findById(super.getCust(request).getCustId());
		return authNetCreditManager.reportAll(cust.getLastProccessId(), cust.getCustId());
	}

	@RequestMapping(value = "/sendReport", method = RequestMethod.POST)
	@ResponseBody
	public JsonResult<String> sendReport(@Valid @RequestBody EmailDto email, HttpServletRequest request)
			throws Exception {
		Cust cust = custManager.findById(super.getCust(request).getCustId());
		authNetCreditManager.sendReport(cust.getLastProccessId(), cust.getCustId(), email.getEmail());
		return new JsonResult<String>("success");
	}

}
